package words;

import java.util.*;

public class SgmlTokSentence {

  TagDefinition m_tagDef;

  int m_nSent;
  // sentence tokens
  String[] m_vecTokens;
  // POS tags
  String[] m_vecPOSTags;
  // DICT tags
  int[] m_vecDICTags;

  // annotation list, ordered by position and length
  LinkedList m_lstAnno;

  public SgmlTokSentence(int nSent, 
			 TagDefinition tagDef, 
			 String[] vecTokens, 
			 LinkedList lstAnno)
  {
    m_nSent = nSent;
    m_tagDef = tagDef;
    m_vecTokens = vecTokens;
    m_lstAnno = lstAnno;

    m_vecPOSTags = null;
    m_vecDICTags = null;
  }


  public void setPOSTags(String[] vecPOSTags)
  {
    m_vecPOSTags = vecPOSTags;
  }


  public void setDICTags(int[] vecDICTags)
  {
    m_vecDICTags = vecDICTags;
  }


  public String toString()
  {
    // initialize out vector
    LinkedList[] vecOut = new LinkedList[m_vecTokens.length];
    for (int i = 0; i < vecOut.length; i++) {
      vecOut[i] = new LinkedList();
      vecOut[i].add(m_vecTokens[i]);
    }

    // augment out vector with annotations
    ListIterator it = m_lstAnno.listIterator();
    while (it.hasNext()) {
      Annotation anno = (Annotation) it.next();
      vecOut[anno.m_nBegin].addFirst(anno.getBegin(m_tagDef));
      vecOut[anno.m_nBegin + anno.m_nLen - 1].addLast(anno.getEnd(m_tagDef));
    }

    // create output string
    StringBuffer sbOut = new StringBuffer();
    for (int j = 0; j < vecOut.length; j++) {
      ListIterator lit = vecOut[j].listIterator();
      while (lit.hasNext()) {
        sbOut.append(lit.next());
        sbOut.append(" ");
      }
    }

    return sbOut.toString();
  }

  public LinkedList getAnnotations(String strTag)
  {
    LinkedList lstAnno = new LinkedList();
    int nId = m_tagDef.getId(strTag);
    
    ListIterator it = m_lstAnno.listIterator();
    while (it.hasNext()) {
      Annotation anno = (Annotation) it.next();
      if (anno.m_nId == nId)
        lstAnno.add(anno);
    }
    
    return lstAnno;
  }
    

  public LinkedList getAnnotations(TreeSet setTags)
  {
    LinkedList lstAnno = new LinkedList();
    
    ListIterator it = m_lstAnno.listIterator();
    while (it.hasNext()) {
      Annotation anno = (Annotation) it.next();
      if (setTags.contains(new Integer(anno.m_nId)))
        lstAnno.add(anno);
    }

    // remove duplicates
    Annotation prev = new Annotation(m_tagDef, "none", -1, -1);
    it = lstAnno.listIterator();
    while (it.hasNext()) {
      Annotation anno = (Annotation) it.next();
      if (anno.compareTo(prev) == 0)
	it.remove();
      else
	prev = anno;
    }
    
    return lstAnno;
  }

  public LinkedList setAnnotations(LinkedList lstAnno)
  {
    LinkedList oldAnno = m_lstAnno;
    m_lstAnno = lstAnno;

    return oldAnno;
  }

  public int size()
  {
    return m_vecTokens.length;
  }

}
